//open Word组件
function openWordComponent(){
	var word;
	try{
   		word = new ActiveXObject("Word.Application");
	}catch(ex){
		alert("您的浏览器创建Word ActiveX控件错误,请检查浏览器的安全设置或者是否正确安装Office套件！");
		return false;
	}
	word.Visible=true;
	word.Activate();
   	return word;
}

function closeWordComponent(word){
	delete word;
}

function insertWordByBookmark(word,name,value,replace){
   var range=word.ActiveDocument.Bookmarks(name).Range;
   if(replace==null||replace==true){
      range.Text=value;
   }else if(replace==false){
      range.InsertAfter(value); 
   }
}

function fetchDateString(dd){
	if(dd){
		return dd.substr(0,10);
	}else{
		return "";
	}
}

/////////Export/////////////
//导出备案报告
function applyExportView(apply,censor1,censor2,exhPorpers){
   var word = openWordComponent();
   if(!word){
   	return ;
   }
   var index=document.location.href.indexOf("/exhibit2")+"/exhibit2".length;
   var ref=document.location.href.substr(0,index)+"/direct/template/apply.doc";
   word.Documents.Open(ref);

	//apply
	insertWordByBookmark(word,"appDate", fetchDateString(apply.applyDate));
	insertWordByBookmark(word,"beginDate",fetchDateString(apply.beginDate));
	insertWordByBookmark(word,"endDate", fetchDateString(apply.endDate));
	insertWordByBookmark(word,"backupCode", apply.applyCode);
	insertWordByBookmark(word,"exhName", apply.exhName);
	insertWordByBookmark(word,"exhAddress", apply.exhAddress);
	insertWordByBookmark(word,"exhArea", apply.exhArea);
	insertWordByBookmark(word,"exhHall", apply.exhHall);
	insertWordByBookmark(word,"preEntName", apply.preEntName);
	insertWordByBookmark(word,"undEntName", apply.undEntName);
	insertWordByBookmark(word,"preContactor", apply.preContactor);
	insertWordByBookmark(word,"undContactor", apply.undContactor);
	insertWordByBookmark(word,"preTelephone", apply.preTelephone);
	insertWordByBookmark(word,"undTelephone", apply.undTelephone);
	insertWordByBookmark(word,"agentEntName", apply.agentEntName);
	insertWordByBookmark(word,"agentContactor", apply.agentContactor);
	insertWordByBookmark(word,"agentTelephone", apply.agentTelephone);
	insertWordByBookmark(word,"agentMobileTelephone", apply.agentMobileTelephone);
	insertWordByBookmark(word,"fax", apply.fax);
	
   	insertWordByBookmark(word,"exhPropDatas", exhPorpers[0]);
	insertWordByBookmark(word,"trePlanDatas", exhPorpers[1]);
	insertWordByBookmark(word,"itemSortDatas", exhPorpers[2]);
	insertWordByBookmark(word,"sheetTypeDatas", exhPorpers[3]);
   
   	//applycensor
	insertWordByBookmark(word,"auditDate", fetchDateString(censor1.updateDate));
	insertWordByBookmark(word,"censorDate", fetchDateString(censor2.updateDate));
	insertWordByBookmark(word,"auditer", censor1.username);
	insertWordByBookmark(word,"censorer", censor2.username);
	if(censor1.censorResult=="3"){
		insertWordByBookmark(word,"auditOpinion", "批准");
	}else if(censor1.censorResult=="5"){
		insertWordByBookmark(word,"auditOpinion", "不批准");
	}
	if(censor2.censorResult=="3"){
		insertWordByBookmark(word,"censorOpinion", "同意");
	}else if(censor2.censorResult=="5"){
		insertWordByBookmark(word,"censorOpinion", "不批准");
	}
	
	
	closeWordComponent(word);
	return ;
}

//导出结案报告
function archiveExportView(apply,inspectDeclMap,simpleDeclList,simpleGoodsList){
	var word = openWordComponent();
   if(!word){
   	return ;
   }
   var index=document.location.href.indexOf("/exhibit2")+"/exhibit2".length;
   var ref=document.location.href.substr(0,index)+"/direct/template/finishReport.doc";
   word.Documents.Open(ref);

	//apply
	insertWordByBookmark(word,"exhName", apply.exhName);
	insertWordByBookmark(word,"exhName1", apply.exhName);
	insertWordByBookmark(word,"agentEntName", apply.agentEntName);
	insertWordByBookmark(word,"backupCode", apply.applyCode);
	insertWordByBookmark(word,"beginDate",fetchDateString(apply.beginDate));
	insertWordByBookmark(word,"endDate", fetchDateString(apply.endDate));
	insertWordByBookmark(word,"exhAddress", apply.exhAddress);
	insertWordByBookmark(word,"exhArea", apply.exhArea);
	insertWordByBookmark(word,"exhHall", apply.exhHall);
	insertWordByBookmark(word,"undEntName", apply.undEntName);
	insertWordByBookmark(word,"preEntName", apply.preEntName);
	insertWordByBookmark(word,"declCount", simpleDeclList.length);
	var d=new Date();
	insertWordByBookmark(word,"nowDate", d.getYear()+"-"+d.getMonth()+"-"+d.getDate());
	
	//inspectDeclMap
	var array=["pList","pCount","qList","qCount","rList","rCount","sList","sCount",
			   "mList","mCount","nList","nCount","lList","lCount","nonList","nonCount"];
	for(var n=0;n<array.length;){
		var name=array[n];
		for(var m=0;m<inspectDeclMap.length;m++){
			var p=inspectDeclMap[m]["string"];
			if(p[0]==name){
				insertWordByBookmark(word,name,p[1]);
				insertWordByBookmark(word,array[n+1],declCount(p[1]));
			}
		}
		n=n+2;
	}
	
	//declList
	var table1=word.ActiveDocument.Tables(1);
	for(var n=0;n<simpleDeclList.length;n++){
		table1.Rows.Add();
		var decl=simpleDeclList[n];
		table1.Cell(n+2,1).Range.Text=n+1;
		table1.Cell(n+2,2).Range.Text=validValue(decl,"declNo");				
		table1.Cell(n+2,3).Range.Text=validValue(decl,"tradeCountryName");
	}
	
	//goodsList
	var table2=word.ActiveDocument.Tables(2);
	for(var n=0;n<simpleGoodsList.length;n++){
		table2.Rows.Add();
		var goods=simpleGoodsList[n];
  		table2.Cell(n+2,1).Range.Text=validValue(goods,"declNo");
		table2.Cell(n+2,2).Range.Text=validValue(goods,"goodsCname");
		table2.Cell(n+2,3).Range.Text=validValue(goods,"inspMonitorCond");
		table2.Cell(n+2,4).Range.Text=validValue(goods,"qty")+validValue(goods,"qtyUnitName")+"/"+validValue(goods,"weight")+validValue(goods,"weightUnitCname");
		table2.Cell(n+2,5).Range.Text=validValue(goods,"goodsClearItem.giveupCount");		
		table2.Cell(n+2,6).Range.Text=validValue(goods,"goodsClearItem.largessCount");
		table2.Cell(n+2,7).Range.Text=validValue(goods,"goodsClearItem.consumeCount");
		table2.Cell(n+2,8).Range.Text=validValue(goods,"goodsClearItem.destroyCount");
		table2.Cell(n+2,9).Range.Text=validValue(goods,"goodsClearItem.transportCount");
		table2.Cell(n+2,10).Range.Text=validValue(goods,"goodsClearItem.buyCount");
		table2.Cell(n+2,11).Range.Text=validValue(goods,"goodsClearItem.returnCount");
		table2.Cell(n+2,12).Range.Text=validValue(goods,"goodsClearItem.punishCount");
		table2.Cell(n+2,13).Range.Text=validValue(goods,"goodsClearItem.loseCount");
		table2.Cell(n+2,14).Range.Text=validValue(goods,"goodsClearItem.otherCount");
		table2.Cell(n+2,15).Range.Text=validValue(goods,"goodsClearItem.declareNo");
		table2.Cell(n+2,16).Range.Text=validValue(goods,"goodsClearItem.passageNo");
		table2.Cell(n+2,17).Range.Text=validValue(goods,"goodsClearItem.balance");
	}
	
	closeWordComponent(word);
	return ;
}

function validValue(obj,str){
	if(obj&&str){
		var subs=str.split(".");
		for(i=0;i<subs.length;i++){
			if(obj[subs[i]]){
				obj=obj[subs[i]];
			}else{
				return "";
			}
		}
		return obj;	
	}else{
		return "";
	}
}

function declCount(str){
	var n=countCharInString(str);
	if(n!=0){
		return n+1;
	}else{
		return n;	
	}
}
